package cn.aixuxi.security.controller;

import cn.aixuxi.security.constant.SysConstant;
import cn.aixuxi.security.entity.SysPermission;
import cn.aixuxi.security.service.SysPermissionService;
import cn.aixuxi.security.service.SysRolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 类描述
 *
 * @author ruozhuliufeng
 * @version 1.0
 * @date 2021/4/22 22:23
 */
@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysRolePermissionService rolePermissionService;
    @GetMapping("index")
    public String index(Model model){
        List<SysPermission> permissionList = permissionService.list();
        model.addAttribute("permissionList",permissionList);
        Integer permissionCount = permissionService.count();
        model.addAttribute("permissionCount",permissionCount);
        return "permission";
    }

    @PostMapping("add")
    public String add(SysPermission permission){
        // 设置可用
        permission.setAvailable(SysConstant.AVAILABLE);
        permissionService.save(permission);
        // 刷新页面
        return "redirect:/permission/index";
    }

    @RequestMapping("update/{id}")
    public String update(@PathVariable("id") Integer id,Model model){
        SysPermission permission = permissionService.getById(id);
        model.addAttribute("updatePermission",permission);
        // 刷新页面
        return "permission_update";
    }

    @PostMapping("update")
    public String update(SysPermission permission){
        permissionService.save(permission);
        // 刷新页面
        return "redirect:/permission/index";
    }
    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        permissionService.removeById(id);
        // 刷新页面
        return "redirect:/permission/index";
    }
}
